package de.midnightdance.admin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchAdvisor;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;

import de.midnightdance.admin.bund.ListenerViewBuende;
import de.midnightdance.admin.bund.ViewBuende;
import de.midnightdance.admin.clan.ListenerViewClans;
import de.midnightdance.admin.clan.ViewClans;
import de.midnightdance.admin.disziplin.ListenerViewDisziplin;
import de.midnightdance.admin.disziplin.ViewDisziplin;
import de.midnightdance.admin.jagdTechnik.ListenerViewJagdTechniken;
import de.midnightdance.admin.jagdTechnik.ViewJagdTechniken;
import de.midnightdance.admin.ritual.ListenerViewRituale;
import de.midnightdance.admin.ritual.ViewRituale;
import de.midnightdance.admin.user.ListenerViewUser;
import de.midnightdance.admin.user.ViewUser;
import de.midnightdance.admin.vorteile.ListenerViewVorteil;
import de.midnightdance.admin.vorteile.ViewVorteile;

public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {

	private static final String PERSPECTIVE_ID = "de.midnightdance.admin.perspective"; //$NON-NLS-1$

    public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
        return new ApplicationWorkbenchWindowAdvisor(configurer);
    }

	public String getInitialWindowPerspectiveId() {
		return PERSPECTIVE_ID;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.application.WorkbenchAdvisor#postStartup()
	 */
	@Override
	public void postStartup() {
		IWorkbenchWindow window = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
		//build list of partListeners
		List<IPartListener> listeners = new ArrayList<IPartListener>();
		initListenerList(listeners);
		//register view listeners
		IPartService partService = (IPartService)window.getService(IPartService.class);		
		Iterator<IPartListener> it = listeners.iterator();
		while (it.hasNext()) {
			IPartListener	listener = (IPartListener) it.next();
			partService.addPartListener(listener);
		}		
		//open views
		List<String> viewIDs = new ArrayList<String>();
		initViewIds(viewIDs);		
		Iterator<String> idIterator = viewIDs.iterator();
		while (idIterator.hasNext()) {
			String viewID = (String) idIterator.next();
			try {
				window.getActivePage().showView(viewID);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}			
	}

	

	/**
	 * will someday be generated by plugIns, when separation to plugIns is done
	 * @param listeners
	 */
	private void initListenerList(List<IPartListener> listeners) {
		listeners.add(new ListenerViewBuende());
		listeners.add(new ListenerViewDisziplin());
		listeners.add(new ListenerViewUser());
		listeners.add(new ListenerViewVorteil());
		listeners.add(new ListenerViewClans());
		listeners.add(new ListenerViewRituale());
		listeners.add(new ListenerViewJagdTechniken());
	}
	/**
	 * will someday be generated by plugIns, when separation to plugIns is done
	 * @param viewIDs
	 */
	private void initViewIds(List<String> viewIDs) {
		viewIDs.add(ViewBuende			.ID);
		viewIDs.add(ViewDisziplin		.ID);
		viewIDs.add(ViewUser			.ID);
		viewIDs.add(ViewVorteile		.ID);
		viewIDs.add(ViewClans			.ID);	
		viewIDs.add(ViewRituale			.ID);
		viewIDs.add(ViewJagdTechniken	.ID);
	}
	
}
